/**
 * 获取网络状态
 */
export function getNetWork() {
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: (res) => {
				console.log(res)
				if(res.networkType=="none"){
					let info = {
						hasNetworkType: false,
						networkType: res.subtype || res.networkType
					}
					resolve(info)
				}else{
					let info = {
						hasNetworkType: true,
						networkType: res.subtype || res.networkType
					}
					resolve(info)
				}
				// if(res.networkType!='none' && res.networkType!='unknown'){
					
				// }
			

			},
			fail: () => {
				let info = {
					hasNetworkType: false				
				}
				reject(info)
			}
		})
	})
}

export function getLocation() {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'wgs84',
			success: (res) => {
				let info = {
							"latitude": res.latitude,
							"longitude": res.longitude,
						}
				resolve(info)

			},
			fail: () => {
				let info = {
					hasNetworkType: false				
				}
				reject(info)
			}
		})
	})
}

export async function getLatlng() {
			let getLatlngOnline = ()=>{
				return new Promise((resolve, reject)=>{
					uni.getLocation({
						type: 'wgs84',
						success: (res)=> {
							resolve({latitude: res.latitude, longitude: res.longitude})
						},
						fail: (error)=> {
							uni.showToast({
								title: '当前位置获取失败,请检查GPS是否打开',
								duration: 2000,
								icon: 'none'
							});
							reject();
						}
					});
				});
			}
			let getLatlngOffline = ()=>{
				return new Promise((resolve, reject)=>{
					plus.geolocation.getCurrentPosition((res)=>{
						resolve({latitude: res.coords.latitude, longitude: res.coords.longitude});
					}, (error)=>{
						uni.showToast({
							title: error.message,
							duration: 2000,
							icon: 'none'
						});
						reject();
					});
				});
			}
			return new Promise((resolve, reject)=>{
				uni.getNetworkType({
					success: async ({networkType})=>{
						try{
							let coord = networkType === 'none'?await getLatlngOffline():await getLatlngOnline();
							resolve(coord);
						}catch(_){
							reject();
						}
					}
				})
			});
}
 export function  getLatlngOffline(){
	 console.log("location")
	 		// plus.geolocation.getCurrentPosition(function(s){console.log(1111,s.coords.latitude)},function(e){console.log(e)})
			// plus.geolocation.getCurrentPosition(function(p){
			// 		console.log('Geolocation\nLatitude:' + p.coords.latitude + '\nLongitude:' + p.coords.longitude + '\nAltitude:' + p.coords.altitude);
			// 	}, function(e){
			// 		console.log('Geolocation error: ' + e.message);
			// 	} );
			
				return new Promise((resolve, reject)=>{
					
					plus.geolocation.getCurrentPosition((res)=>{
						console.log("ss",res)
						resolve({latitude: res.coords.latitude, longitude: res.coords.longitude});
					}, (error)=>{
						uni.showToast({
							title: error.message,
							duration: 2000,
							icon: 'none'
						});
						reject();
					});
				});
			}